<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2018/12/5
  Time: 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<script src="<c:url value="/resources/bootstrap/js/bootstrap-datetimepicker.min.js" />"></script>
<script src="<c:url value="/resources/bootstrap/js/bootstrap-datetimepicker.zh-CN.js" />"></script>
<link rel="stylesheet" href="<c:url value="/resources/bootstrap/css/bootstrap-datetimepicker.css" />">

<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-trophy"></i> 计划工单
            </div>
            <div class="box-tools pull-right">
                <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_EDIT')">
                    <%--<a href="#" class="btn btn-default" id="btmExport" title="导出"><i class="fa fa-filter"></i> 导出</a>--%>
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerWindow('wPlannerOrder','<c:url
                            value="/web/admin/plannerInspection/editPage.action"/>','新增',['80%', '95%'])"
                       class="btn btn-default btnTool" title="新增计划工单"><i class="fa fa-plus"></i> 新增周期性计划</a>
                    <a href="javascript:;" onclick="copyEdit()"
                       class="btn btn-default btnTool" title="复制新增"><i class="fa fa-plus"></i> 复制新增</a>
                </sec:authorize>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_ASSIGN')">
                    <a href="javascript:;" onclick="assign(0)"
                       class="btn btn-default btnTool" title="指定接单人"><i class="fa fa-plus"></i> 指定接单人</a>
                    <a href="javascript:;" onclick="assign(1)"
                       class="btn btn-default btnTool" title="指定接单人"><i class="fa fa-plus"></i> 指定岗位</a>
                </sec:authorize>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_OPERATE')">
                    <a href="javascript:;" onclick="startPlanner()"
                       class="btn btn-default btnTool" title="开始计划"><i class="icon-control-play"></i> 开始计划</a>
                    <a href="javascript:;" onclick="endPlanner()"
                       class="btn btn-default btnTool" title="停止计划"><i class="icon-control-pause"></i> 停止计划</a>
                </sec:authorize>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_DATE')">
                    <a href="javascript:;" onclick="assignDate()"
                       class="btn btn-default btnTool" title="强制生成指定日实施单"><i class="icon-control-end"></i>
                        强制生成指定日实施单</a>
                </sec:authorize>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_RELATIVE')">
                    <a href="javascript:;" onclick="relativeOrder()"
                       class="btn btn-default btnTool" title="查看生成工单"><i class="glyphicon glyphicon-eye-open"></i>
                        查看生成工单</a>
                </sec:authorize>
                <%--<sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_IMPORT')">--%>
                <%--<a href="javascript:;"--%>
                <%--class="btn btn-default btnTool" title="导入计划"><i class="icon-folder-alt"></i> 导入计划</a>--%>
                <%--<a href="javascript:;"--%>
                <%--class="btn btn-default btnTool" title="导入模板下载"><i class="icon-folder-alt"></i> 导入模板下载</a>--%>
                <%--</sec:authorize>--%>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_EXPORT')">
                    <a href="javascript:;"
                       class="btn btn-default btnTool"
                       onclick="vLayuiUtil.openLayerIframWindow('wYearPlan','<c:url
                               value="/web/admin/plannerInspection/organizationView.action"/>','选择部门',['80%', '95%'])"
                       title="导出年度计划"><i class="icon-book-open"></i> 导出年度计划</a>
                </sec:authorize>
            </div>
        </div>

        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/plannerInspection/listJson.action"/>" method="post"
                  class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">工单名称：</label>
                    <div class="control-inline">
                        <input type="text" id="name" name="name" value="" maxlength="64"
                               class="form-control width-120">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">时间周期：</label>
                    <div class="control-inline">
                        <input type="text" id="period" name="period" value="" maxlength="64"
                               class="form-control width-120">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">开始时间：</label>
                    <div class="control-inline">
                        <input type="text" id="startTime" name="start" value="" maxlength="64"
                               class="form-control Wdate" autoComplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">结束时间：</label>
                    <div class="control-inline">
                        <input type="text" id="endTime" name="end" value="" maxlength="64"
                               class="form-control Wdate" autoComplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工作要求：</label>
                    <div class="control-inline">
                        <input type="text" id="required" name="required" value="" maxlength="64"
                               class="form-control width-120">
                    </div>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>

            <table id="dataGrid"></table>
            <div id="dataGridPage"></div>
        </div>
    </div>
</div>
<%--//模态框--%>
<%--<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">--%>
<%--<div class="modal-dialog" role="document">--%>
<%--<div class="modal-content">--%>
<%--<div class="modal-header">--%>
<%--<button type="button" class="close" data-dismiss="modal" aria-label="Close">--%>
<%--<span aria-hidden="true">×</span>--%>
<%--</button>--%>
<%--<h4 class="modal-title" id="myModalLabel">Modal title</h4>--%>
<%--</div>--%>
<%--<div class="modal-body">--%>
<%--<div class="col-xs-6">--%>
<%--<div class="form-group">--%>
<%--<label class="control-label col-sm-4" title=""><span class="required ">*</span>--%>
<%--结束时间：<i class="fa icon-question hide"></i></label>--%>
<%--<div class="col-sm-8">--%>
<%--<input name="end" id="end"--%>
<%--class="form-control Wdate required">--%>
<%--</div>--%>
<%--</div>--%>
<%--</div>--%>
<%--</div>--%>
<%--<div class="modal-footer">--%>
<%--<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>--%>
<%--<button type="button" class="btn btn-primary">Save</button>--%>
<%--</div>--%>
<%--</div>--%>
<%--</div>--%>
<%--</div>--%>
<script>
    var selectIds = [];
    $('#dataGrid').dataGrid({
        searchForm: $("#searchForm"),
        showCheckbox: true,
        columnModel: [
            <sec:authorize access="hasRole('ROLE_ENTERPRISE_INSPECTION_PLANNER_ORDER_EDIT')">
            {
                header: '操作',
                name: 'actions',
                width: 100,
                frozen:true,
                align: "center",
                sortable: false,
                title: false,
                formatter: function (val, obj, row, act) {
                    return '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" submitRemove(\'' + row.id + '\', \'0\')"><i class="fa fa-trash-o"></i>删除</a>&nbsp;'
                        +'<a class="btn btn-default btn-xs btnList" onclick="editPlannerOrder(' + row.id + ')" title="编辑"><i class="fa fa-pencil"></i>编辑</a>&nbsp;'
                }
            },
            </sec:authorize>
            {header: 'id', name: 'id', index: 'id', width: 50, align: "center", edittype: "checkbox",sortable: false},
            {header: '流水号', name: 'serial', index: 'serial', width: 80, align: "center",sortable: false},
            {header: '工单名称', name: 'name', index: 'name', width: 100, align: "center",sortable: false},
            {header: '是否指派', name: 'assignType', index: 'assignType', width: 60, align: "center",sortable: false},
            {header: '计划周期', name: 'installName', index: 'installName', width: 80, align: "center",sortable: false},
            {header: '运行状态', name: 'plannerStatus', index: 'plannerStatus', width: 60, align: "center",sortable: false},
            {header: '开始时间', name: 'startTime', index: 'startTime', width: 100, align: "center",sortable: false},
            {header: '结束时间', name: 'endTime', index: 'endTime', width: 100, align: "center",sortable: false},
            {header: '提醒时间', name: 'remindTime', index: 'remindTime', width: 60, align: "center",sortable: false},
            {header: '工作要求', name: 'required', index: 'required', width: 200, align: "center",sortable: false},
            {header: '所属企业', name: 'enterpriseName', index: 'enterpriseName', width: 80, align: "center",sortable: false},
            {header: '创建时间', name: 'createDate', index: 'createDate', width: 100, align: "center",sortable: false}
        ],
        // onselectRow:function(id){
        // var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        // if(ids.contains(id)){
        //
        // }
        // },
        // 加载成功后执行事件
        ajaxSuccess: function (data) {

        },
    });
    $("#dataGrid").dataGrid('setFrozenColumns');


    function editPlannerOrder(id) {

        vLayuiUtil.openLayerWindow('wPlannerOrder', '<c:url
        value="/web/admin/plannerInspection/editPage.action?id="/>' + id, '编辑', ['80%', '95%'])
    }


    function submitRemove(id) {
        js.confirm('你确认要废弃这条数据吗？', function () {
            //进度条
            js.loading("请等待。。。。");
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/plannerInspection/delete.action"/>",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.closeLoading();
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    //指定岗位和人员 type:0人员，1：岗位
    function assign(type) {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要派单的巡检计划");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条维保计划进行派单");
            return false;
        }
        var flag = 0;
        $.each(ids, function (i, item) {
            var ret = $("#dataGrid").jqGrid('getRowData', item);
            var assignType = ret.assignType;
            console.log(assignType);
            if (type == 0 && assignType == "指派岗位") {
                flag = 1;
            }
            if (type == 1 && assignType == "指派人员") {
                flag = 2;
            }
        });
        if (flag == 1) {
            js.showMessage("存在已经指派岗位的计划工单，不能再指派人员");
            return false;
        }
        if (flag == 2) {
            js.showMessage("存在已经指派人员的计划工单，不能再指派岗位");
            return false;
        }

        if (type == 0) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignUser', '<c:url
        value="/web/admin/plannerInspection/assignUser.action?plannerInspectionId="/>' + ids, '指定接单人')
        }
        if (type == 1) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignPosition', '<c:url
        value="/web/admin/plannerInspection/assignPosition.action?plannerInspectionId="/>' + ids, '指定岗位')
        }

    }

    //开始计划
    function startPlanner() {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要开始计划的巡检计划");
            return false;
        }
        $.ajax({
            type: 'post',
            url: "<c:url value="/web/admin/plannerInspection/startPlanner.action"/>",
            data: {"ids": JSON.stringify(ids)},
            dataType: 'json',
            async: false,
            success: function (data) {
                js.showMessage(data.msg);
                $('#dataGrid').dataGrid('refresh');
            },
            error: function () {
                js.showMessage("出错了，请联系管理员");
            }
        });
    }

    //停止计划
    function endPlanner() {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要停止计划的巡检计划");
            return false;
        }
        $.ajax({
            type: 'post',
            url: "<c:url value="/web/admin/plannerInspection/endPlanner.action"/>",
            data: {"ids": JSON.stringify(ids)},
            dataType: 'json',
            async: false,
            success: function (data) {
                js.showMessage(data.msg);
                $('#dataGrid').dataGrid('refresh');
            },
            error: function () {
                js.showMessage("出错了，请联系管理员");
            }
        });
    }

    //查看生成工單
    function relativeOrder() {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要查看巡检单的计划工单");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条巡检计划查看");
            return false;
        }
        vLayuiUtil.openLayerIframMaxWindow('wRelativeOrder', '<c:url
        value="/web/admin/plannerInspection/orderList.action?id="/>' + ids, '查看生成工单')
    }

    function assignDate() {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要操作的计划工单");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条巡检计划操作");
            return false;
        }
        vLayuiUtil.openLayerWindow('wAssignDate', '<c:url
        value="/web/admin/plannerInspection/assignDatePage.action?plannerId="/>' + ids, '强制生成指定日实施单');
    }
    function copyEdit() {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要操作的计划工单");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条巡检计划操作");
            return false;
        }
        // 复制新增前对时间进行判断
        <%--$.ajax({--%>
            <%--type: 'post',--%>
            <%--url: "<c:url value="/web/admin/plannerInspection/beforcopyEditPageCheck.action"/>",--%>
            <%--data: {"id": ids[0]},--%>
            <%--dataType: 'json',--%>
            <%--async: false,--%>
            <%--success: function (data) {--%>
                <%--console.log(data)--%>
                <%--if (data.status == "1") {--%>
                    <%--js.showMessage("复制计划时间必须大于当前时间");--%>
                <%--} else {--%>
                    <%--vLayuiUtil.openLayerWindow('wPlannerOrder','<c:url--%>
                            <%--value="/web/admin/plannerInspection/copyEditPage.action?id="/>'+ids,'复制新增',['80%', '95%'])--%>
                <%--}--%>
            <%--},--%>
            <%--error: function () {--%>
                <%--js.showMessage("出错了，请联系管理员");--%>
            <%--}--%>
        <%--});--%>

        vLayuiUtil.openLayerWindow('wPlannerOrder','<c:url
                            value="/web/admin/plannerInspection/copyEditPage.action?id="/>'+ids,'复制新增',['80%', '95%'])
    }


    $("#startTime").datetimepicker({
        format: 'yyyy-mm-dd',
        language:'zh-CN',
        minView: "month",//设置只显示到月份
        autoclose:true,//选中关闭
        todayBtn: true//今日按钮
    });
    $("#endTime").datetimepicker({
        format: 'yyyy-mm-dd',
        language:'zh-CN',
        minView: "month",//设置只显示到月份
        autoclose:true,//选中关闭
        todayBtn: true//今日按钮
    });

    // 时间选择器互动（结束时间不得早于开始时间）
    $("#startTime").on('changeDate', function(ev) {
        $("#endTime").datetimepicker('setStartDate', ev.date);
        var d = $("#endTime").val();
        if (d) {
            var date = new Date(d.replace(/-/g, '/'));
            if (date != 'Invalid Date' && date < ev.date) {
                $("#endTime").datetimepicker('setDate', ev.date)
            }
        }
    });
</script>
